################################################################################
##
## Purpose: This script creates SI Figure 4
##
## Author: James Bisbee (james.h.bisbee@vanderbilt.edu)
##
## Input Files:
## Output Files:
##  - ./output/figures/SI_figure_4.pdf
##
##
## See associated log file for compute environment, package versions, 
##  and date of most recent run.
##
################################################################################
rm(list = ls())
gc()
require(tidyverse)
require(ggridges)

set.seed(123)

# Compute details
print(paste0('Compute environment from ',Sys.Date(),' run by Bisbee'))
if(Sys.info()['sysname'] == 'Windows') {
  ram_size = system("wmic MemoryChip get Capacity", intern = TRUE)[-1]
  model_name = system("wmic cpu get name", intern = TRUE)[2] # nocov
  vendor_id = system("wmic cpu get manufacturer", intern = TRUE)[2] # nocov
  
  print(list(ram = stringr::str_squish(ram_size)[1],
             vendor_id = stringr::str_squish(vendor_id),
             model_name = stringr::str_squish(model_name),
             no_of_cores = parallel::detectCores()))
} else if(Sys.info()['sysname'] == 'Linuxs') {
  splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
  df <- do.call(rbind, lapply(splitted[-1], 
                              function(x) data.frame(
                                cpu = as.numeric(x[2]),
                                mem = as.numeric(x[4]),
                                pid = as.numeric(x[5]),
                                cmd = paste(x[-c(1:5)], collapse = " "))))
  df
} else {
  cat("If not on Linux or Windows, you'll have to figure out your own solution to seeing the compute environment.")
}

sessionInfo()


toplot <- data.frame(Descriptive = dnorm(seq(-10,20,by = .1),2,2),
           Injunctive = dnorm(seq(-10,20,by = .1),6,2),
           x2 = seq(-10,20,by = .1),
           type = 'Equally Salient') %>%
  bind_rows(data.frame(Descriptive = dnorm(seq(-10,20,by = .1),2,.5),
                       Injunctive = dnorm(seq(-10,20,by = .1),6,2),
                       x2 = seq(-10,20,by = .1),
                       type = 'Salient Descriptive')) %>%
  bind_rows(data.frame(Descriptive = dnorm(seq(-10,20,by = .1),2,2),
                       Injunctive = dnorm(seq(-10,20,by = .1),6,.5),
                       x2 = seq(-10,20,by = .1),
                       type = 'Salient Injunctive')) %>%
  bind_rows(data.frame(Descriptive = dnorm(seq(-10,20,by = .1),0,.5),
                       Injunctive = dnorm(seq(-10,20,by = .1),8,.5),
                       x2 = seq(-10,20,by = .1),
                       type = 'Dissimilar Salient Norms')) %>%
  bind_rows(data.frame(Descriptive = dnorm(seq(-10,20,by = .1),3,3),
                       Injunctive = dnorm(seq(-10,20,by = .1),5,3),
                       x2 = seq(-10,20,by = .1),
                       type = 'Similar Non-Salient Norms')) %>%
  as_tibble() %>%
  gather(concept,dens,-x2,-type) %>%
  group_by(concept,type) %>%
  mutate(dens = scales::rescale(dens,to = c(0,1))) %>%
  mutate(lab = ifelse(concept == 'Descriptive','delta[i]','rho[i]')) 

toplotSimpl <- toplot %>%
  filter(!grepl('Descriptive|Injunctive',type)) %>%
  mutate(type = factor(type,levels = c('Equally Salient','Dissimilar Salient Norms','Similar Non-Salient Norms')))


pdf('./output/figures/SI_figure_4.pdf',width = 7,height = 5)
toplotSimpl %>%
  ggplot(aes(x = x2,y = dens,color = concept,label = lab,linetype = concept)) + 
  geom_line() + 
  facet_wrap(~type,ncol = 1) + 
  geom_vline(data = toplotSimpl %>% group_by(concept,type,lab) %>% summarise(x2 = weighted.mean(x2,dens)),
             aes(xintercept = x2)) + 
  geom_label(data = toplotSimpl %>% group_by(concept,type,lab) %>% summarise(x2 = weighted.mean(x2,dens),dens = .1),
             parse = T,color = 'black') + 
  theme_ridges() +
  scale_x_continuous(limits = c(-7,15),breaks = NULL) + 
  scale_y_continuous(breaks=NULL) + 
  xlab(c('Observed Behavior' = expression(x[j]~y[j]))) + 
  labs(title = NULL,
       subtitle = '',
       y = 'Utility') + 
  scale_color_manual(name = 'Norm',values = c('black','grey60')) + 
  scale_linetype_manual(name = "Norm",values = c('solid','dashed'))
dev.off()

# EOF